'use strict';
module.exports = {
	/**
	 * 检查最新的invite_code和tfid,如果有变化就更新用户信息
	 */
	main: async (event) => {
		const db = uniCloud.database();

		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			_
		} = util;
		let {
			uid,
			invite_code,
			tfid,
			clickid,
			advertiser_id
		} = data;
		let res = {
			code: 0,
			msg: ""
		};
		// 业务逻辑开始-----------------------------------------------------------
		var nowtime = new Date().getTime()


		// 查询代理商的详细信息，进而获取机构id
		let agent_info = await db.collection('uni-id-users').doc(invite_code).get()
		let middleman_id = 0;
		let clue_token = 0;
		if (agent_info.data.length == 1) {
			middleman_id = agent_info.data[0].middleman_id;
		}

		//如果从本地带来了clickid,就一并更新到数据库中
		if (clickid.length > 10) {
			if (userInfo.tg_pt == "douyin") {
				clue_token = clickid;
			}
			let update1 = await db.collection('uni-id-users').doc(uid).update({
				register_date:new Date().getTime(),
				middleman_id: middleman_id,
				invite_code: invite_code,
				tfid: tfid,
				clickid: clickid,
				clue_token: clue_token, //如果是抖音平台，且从本地带来了clickid，则需要更新clue_token
				ad_id: advertiser_id //广告账号ID
			})
		} else {
			//如果没有带进来clickid，还需要先去数据库查一遍
			//未绑定则继续执行：通过客户端IP查找jl_chudian里最后一条此IP的callback,且必须是一个小时内记录的，并通过此callback回传信息给巨量
			let ip = originalParam.context.CLIENTIP;
			let chudian_info = await db.collection('jl_chudian').where({
				ip: ip,
				time: _.gt(nowtime - 3600000)
			}).orderBy('time', 'desc').limit(1).get()
			let chudian_info1 = await db.collection('jl_chudian').where({
				wechat_openid: userInfo.wx_openid['mp-weixin'],
				time: _.gt(nowtime - 3600000)
			}).orderBy('time', 'desc').limit(1).get()
			
			

			if (chudian_info.data.length === 1) {
				res.tg_pt = chudian_info.data[0].pt || "pt"; //平台
				res.ad_id = chudian_info.data[0].ad_id || "ad_id"; //广告主账号ID
				res.aid = chudian_info.data[0].aid || "aid"; //广告计划id
				res.cid = chudian_info.data[0].cid || "cid"; //广告创意id
				res.clickid = chudian_info.data[0].callback || "";
				//有clickid和ad_id时，绑定到用户信息里面
				let bind_adid = await db.collection('uni-id-users').doc(userInfo._id).update({
					register_date:new Date().getTime(),
					middleman_id: middleman_id,
					invite_code: invite_code,
					tfid: tfid,
					tg_pt: res.tg_pt,
					ad_id: res.ad_id, //广告主账号ID
					aid: res.aid, //广告计划id
					cid: res.cid, //广告创意id
					clickid: res.clickid,
					pageId: chudian_info.data[0].pageId //oppo广告专属-网页ID
				})
			} else if(chudian_info1.data.length === 1) {
				res.tg_pt = chudian_info1.data[0].pt || "pt"; //平台
				res.ad_id = chudian_info1.data[0].ad_id || "ad_id"; //广告主账号ID
				res.aid = chudian_info1.data[0].aid || "aid"; //广告计划id
				res.cid = chudian_info1.data[0].cid || "cid"; //广告创意id
				res.clickid = chudian_info1.data[0].callback || "";
				//有clickid和ad_id时，绑定到用户信息里面
				let bind_adid = await db.collection('uni-id-users').doc(userInfo._id).update({
					register_date:new Date().getTime(),
					middleman_id: middleman_id,
					invite_code: invite_code,
					tfid: tfid,
					tg_pt: res.tg_pt,
					ad_id: res.ad_id, //广告主账号ID
					aid: res.aid, //广告计划id
					cid: res.cid, //广告创意id
					clickid: res.clickid,
				})
			} else {
				let update2 = await db.collection('uni-id-users').doc(uid).update({
					register_date:new Date().getTime(),
					middleman_id: middleman_id,
					invite_code: invite_code,
					tfid: tfid
				})
			}
		}


		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}